package com.starbucks.opencase.function;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.data.daPath;
import com.starbucks.opencase.data.daScan;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMMark;
import com.starbucks.opencase.enums.enumCMTip;
import com.starbucks.opencase.modualr.moPetCarry;
import com.starbucks.opencase.tool.toolUserGoods;
import com.starbucks.opencase.tool.toolUserPet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class funCarry {
    @Autowired
    private com.starbucks.opencase.tool.toolUserGoods toolUserGoods;
    @Autowired
    private com.starbucks.opencase.tool.toolUserPet toolUserPet;
    @Autowired
    private funSession funSession;
    /**
     * 是否在运输
     * @param uid
     * @param pid
     * @return
     */
    public boolean isCarry(String uid,String pid){
        for(Long l: daScan.carry.keySet()){
            moPetCarry mo=daScan.carry.get(l);
            if(mo.getUid().equals(uid) && mo.getPid().equals(pid))return true;
        }
        return false;
    }

    /**
     * 获取运输的物品id
     * @param uid
     * @param pid
     * @return
     */
    public String getGId(String uid,String pid){
        for(Long l:daScan.carry.keySet()){
            moPetCarry mo=daScan.carry.get(l);
            if(mo.getPid().equals(pid) && mo.getUid().equals(uid))return mo.getGid();
        }
        return "";
    }
    public long getEndTime(String uid,String pid){
        for(Long l:daScan.carry.keySet()){
            moPetCarry mo=daScan.carry.get(l);
            if(mo.getPid().equals(pid) && mo.getUid().equals(uid))return mo.getEnd();
        }
        return 0;
    }

    /**
     * 是否运输结束
     * @param uid
     * @param pid
     * @return
     */
    public boolean isCarryEnd(String uid,String pid){
        for(Long l:daScan.carry.keySet()){
            moPetCarry mo=daScan.carry.get(l);
            if(mo.getPid().equals(pid) && mo.getUid().equals(uid)){
                return System.currentTimeMillis()>=mo.getEnd();
            }
        }
        return false;
    }

    /**
     * 移出
     * @param uid
     * @param pid
     */
    public void remove(String uid,String pid){
        for(Long l:daScan.carry.keySet()){
            moPetCarry mo=daScan.carry.get(l);
            if(mo.getPid().equals(pid) && mo.getUid().equals(uid)){
                daScan.carry.remove(l);
                return;
            }
        }
    }
    public void remove(long bid){
        daScan.carry.remove(bid);
    }
    /**
     * 随机抽取对手
     * @param uid
     * @return
     */
    public moPetCarry randomGet(String uid){
        ArrayList<Long> a=new ArrayList<>();
        for(Long l:daScan.carry.keySet()){
            moPetCarry mo=daScan.carry.get(l);
            if(uid.equals(mo.getUid()))continue;
            if(daScan.battle.containsKey(l))continue;
            a.add(l);
        }
        if(a.isEmpty())return null;
        return daScan.carry.get(a.get(new Random().nextInt(a.size())));
    }

    /**
     * 获取运输状态
     * @param id
     * @return
     */
    public moPetCarry query(Long id){
        if(!daScan.carry.containsKey(id))return null;
        return daScan.carry.get(id);
    }
    /**
     * 添加背回数据
     * @param carry
     */
    public void addCarry(moPetCarry carry){
        Timer timer=new Timer();
        timer.schedule(new moCarry(carry.getStart()),carry.getEnd()- carry.getStart());
        carry.setTimer(timer);
        daScan.carry.put(carry.getStart(), carry);
    }
    public void addRobot(String uid){
        if(daScan.robot.contains(uid))daScan.robot.add(uid);
    }
    public void removeRobot(String uid){
        daScan.robot.remove(uid);
    }
    /**
     * 查询离线时被劫镖失败结果
     * @param uid
     * @return
     */
    public List<moPetCarry> queryEndList(String uid){
        ArrayList<moPetCarry> as=new ArrayList<>();
        for(Long l:daScan.lose.keySet()){
            moPetCarry mo=daScan.lose.get(l);
            if(mo.getUid().equals(uid))as.add(mo);
        }
        return as;
    }
    /**
     * 暂停运输
     * @param id
     */
    public void pauseCarry(long id){
        moPetCarry carry=query(id);
        daScan.carry.get(id).getTimer().cancel();
        daScan.battle.put(id,System.currentTimeMillis()- carry.getStart());
        Timer t=new Timer();
        t.schedule(new auto_pause(id),60_000*30);
        daScan.battle_auto_end.put(id,t);
    }

    /**
     * 继续运输
     */
    public void startCarry(long id){
        moPetCarry carry=query(id);
        long t=System.currentTimeMillis();
        long q=t-(daScan.battle.get(id)+ carry.getStart());//被耽搁的时间
        carry.setEnd(carry.getEnd()+q);
        Timer timer=new Timer();
        timer.schedule(new moCarry(id),carry.getEnd()- System.currentTimeMillis());
        carry.setTimer(timer);
        daScan.carry.put(id,carry);
        daScan.battle.remove(id);
    }

    /**
     * 结束运输
     * @param bid
     */
    public void stopCarry(long bid){
        daScan.battle_auto_end.get(bid).cancel();
        daScan.battle_auto_end.remove(bid);
        daScan.battle.remove(bid);
        daScan.carry.remove(bid);
    }
    public int carryNum(){
        return daScan.carry.size();
    }
    public int carryRobNum(){
        return daScan.battle.size();
    }
    public class auto_pause extends TimerTask{
        private long t;
        public auto_pause(long bid){
            t=bid;
        }

        @Override
        public void run() {
            startCarry(t);
        }
    }
    public class moCarry extends TimerTask {
        private long t;
        public moCarry(long t){
            this.t=t;
        }
        @Override
        public void run() {
            moPetCarry mo=daScan.carry.get(t);
            ObjectNode on=new ObjectMapper().createObjectNode();
            on.put(enumCMKey.mark.name(), enumCMMark.carryEnd.ordinal());
            on.put(enumCMKey.timestamp.name(), System.currentTimeMillis());
            on.put(enumCMKey.pid.name(), mo.getPid());
            on.put(enumCMKey.pname.name(),toolUserPet.queryName(mo.getPid()));
            on.put(enumCMKey.purl.name(), daPath.getPet(mo.getPid()));
            on.put(enumCMKey.gid.name(), mo.getGid());
            on.put(enumCMKey.gname.name(), toolUserGoods.queryName(mo.getGid()));
            on.put(enumCMKey.gurl.name(), daPath.getGoods(mo.getGid()));
            on.put(enumCMKey.status.name(), true);
            on.put(enumCMKey.tip.name(), enumCMTip.success_carry_end.getTip());
            funSession.sendMessage(funSession.getIdOfUId(mo.getUid()), on);
        }
    }
}
